<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title></title>
  </head>
  <body>
    <div id="app">
      <div>
        已出版书籍：<span>{{ person.books.length > 0 ? 'Yes' : 'No' }}</span>
      </div>
      <div>已出版书籍：<span>{{ booksMessage }}</span></div>
      <div>已出版书籍：<span>{{ booksMessageFn() }}</span></div>
      <div>test: {{ updateData }}</div>
    </div>

    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

    <script>
      const { createApp, reactive, computed, ref } = Vue;

      const app = createApp({
        setup() {
          const person = reactive({
            name: "编程段子",
            books: ["HTML", "CSS", "JavaScript"],
          });

          const updateData = ref("test");

          const booksMessage = computed(() => {
            // 如果 依赖的相关数据改变，比如这里 person.books 改变，booksMessage 才会更新
            console.log("computed update", +new Date());
            return person.books.length > 0 ? "Yes" : "No";
          });

          const booksMessageFn = () => {
            // 如果 当前(组件)模板改变，booksMessageFn 就会重新执行一次
            console.log("method update", +new Date());
            return person.books.length > 0 ? "Yes" : "No";
          };

          // 计算属性会更新，方法也会更新
          setTimeout(() => person.books.push("Vue.js"), 1000);

          // 计算属性不会更新，方法也会更新
          setTimeout(() => (updateData.value = "test2"), 2000);

          // 总结：
          // 计算属性值会基于其响应式依赖更新而更新
          // 方法会基于(组件)模板更新而更新

          return {
            person,
            booksMessage,
            booksMessageFn,
            updateData,
          };
        },
      });

      app.mount("#app");
    </script>
  </body>
</html>
